package com.cskaoyan.order.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @USER: cuiweiguo#
 * @DATE: 2022/5/20
 * @TIME: 10:10
 * @PROJECT_NAME: duolaimall
 */
@Service
@Slf4j
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderConverter orderConverter;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        Integer page = request.getPage();
        Integer size = request.getSize();
        String sort = request.getSort();
        Long userId = request.getUserId();
        OrderListResponse orderListResponse = new OrderListResponse();
        // 查询订单详情

        // 分页
        PageHelper.startPage(page, size);

        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        if (!StringUtils.isEmpty(sort)) {
            example.setOrderByClause(sort);
        } else {
            example.setOrderByClause("create_time DESC");
        }
        List<Order> orders = orderMapper.selectByExample(example);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);

        // 转换
        List<OrderDetailInfo> orderDetailInfos = new ArrayList<>();
        for (Order order : orders) {
            OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);
            // 查询orderItem
            List<OrderItemDto> orderItemDto = getOrderItemDtos(order.getOrderId());
            orderDetailInfo.setOrderItemDto(orderItemDto);

            // 查询shipping
            OrderShippingDto orderShippingDto = getOrderShippingDto(order.getOrderId());
            orderDetailInfo.setOrderShippingDto(orderShippingDto);
            orderDetailInfos.add(orderDetailInfo);
        }
        orderListResponse.setData(orderDetailInfos);
        orderListResponse.setTotal(orderPageInfo.getTotal());

        orderListResponse.setCode(OrderRetCode.SUCCESS.getCode());
        orderListResponse.setMsg(OrderRetCode.SUCCESS.getMessage());

        return orderListResponse;
    }

    private OrderShippingDto getOrderShippingDto(String orderId) {
        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
        return orderConverter.shipping2dto(orderShipping);
    }

    private List<OrderItemDto> getOrderItemDtos(String orderId) {
        Example orderItemExample = new Example(OrderItem.class);
        orderItemExample.createCriteria().andEqualTo("orderId", orderId);
        List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);
        return orderConverter.item2dto(orderItems);
    }

    @Override
    public OrderDetailResponse orderDetail(OrderDetailRequest request) {
        String orderId = request.getOrderId();
        Order order = orderMapper.selectByPrimaryKey(orderId);
        OrderDetailResponse orderDetailResponse = orderConverter.order2res(order);
        List<OrderItemDto> orderItemDto = getOrderItemDtos(order.getOrderId());
        orderDetailResponse.setOrderItemDto(orderItemDto);
        OrderShippingDto orderShippingDto = getOrderShippingDto(order.getOrderId());
        orderDetailResponse.setOrderShippingDto(orderShippingDto);

        orderDetailResponse.setCode(OrderRetCode.SUCCESS.getCode());
        orderDetailResponse.setMsg(OrderRetCode.SUCCESS.getMessage());

        return orderDetailResponse;
    }

    @Override
    public OrderCountResponse orderCount(OrderCountRequest request) {
        Example example = new Example(Order.class);
        example.createCriteria().andEqualTo("userId", request.getUserId());
        int count = orderMapper.selectCountByExample(example);
        OrderCountResponse orderCountResponse = new OrderCountResponse();
        orderCountResponse.setCount(count);
        orderCountResponse.setCode(OrderRetCode.SUCCESS.getCode());
        orderCountResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        return orderCountResponse;
    }

    @Override
    public OrderItemResponse orderItem(OrderItemRequest request) {
        String orderItemId = request.getOrderItemId();

        OrderItem orderItem = orderItemMapper.selectByPrimaryKey(orderItemId);
        OrderItemResponse orderItemResponse = orderConverter.item2res(orderItem);

        Order order = orderMapper.selectByPrimaryKey(orderItem.getOrderId());
        OrderDto orderDto = orderConverter.order2dto(order);
        orderItemResponse.setOrderDto(orderDto);

        orderItemResponse.setCode(OrderRetCode.SUCCESS.getCode());
        orderItemResponse.setMsg(OrderRetCode.SUCCESS.getMessage());

        return orderItemResponse;
    }
}
